package com.nektos.ccf.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.nektos.ccf.shared.Volunteer;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class Registration implements EntryPoint, SubmitHandler {
    

	interface Driver extends SimpleBeanEditorDriver<Volunteer, VolunteerForm> {}
    Driver driver = GWT.create(Driver.class);
    VolunteerServiceAsync service = GWT.create(VolunteerService.class);
    
	private VolunteerForm volunteerForm;


	@Override
	public void onModuleLoad() {
        RootPanel rootPanel = RootPanel.get();
        
        volunteerForm = new VolunteerForm();
        volunteerForm.setSubmitHandler(this);
        rootPanel.add(volunteerForm, 10, 10);
        volunteerForm.setSize("700px", "953px");
       
	    // Initialize the driver with the top-level editor
	    driver.initialize(volunteerForm);
	    // Copy the data in the object into the UI
	    driver.edit(new Volunteer());
	}


	@Override
	public void handleSubmit() throws Exception {
	    Volunteer volunteer = driver.flush();
        if(driver.hasErrors()) {
          throw new Exception(driver.getErrors().toString());
        } else {
          service.create(volunteer, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
		        ConfirmPopup popup = new ConfirmPopup("Your volunteer registration has been successfully submitted.  Thank you for supporting this event!");
                popup.show();
                popup.center();

			}
			
			@Override
			public void onFailure(Throwable caught) {
                ErrorPopup popup = new ErrorPopup("Error: "+caught.getMessage());
                popup.show();
                popup.center();
			}
		});
        }				
	}

    private class ConfirmPopup extends DialogBox {
        public ConfirmPopup(String message) {
            
            PushButton button = new PushButton("OK", new ClickHandler() {
    			@Override
    			public void onClick(ClickEvent event) {
                    ConfirmPopup.this.hide();
                    Window.open("/", "_self", null);
    			}
    		});
            button.setSize("77px", "29px");
            VerticalPanel panel = new VerticalPanel();
            panel.add(new Label(message));
            panel.add(button);
            setWidget(panel);
    //        setHeight("250px");
            setWidth("400px");
            setText(" ");
            setStyleName("gwt-DialogBox");
            setGlassEnabled(true);
    	}
        
    }
    private class ErrorPopup extends DialogBox {
        public ErrorPopup(String message) {
            
            PushButton button = new PushButton("OK", new ClickHandler() {
    			@Override
    			public void onClick(ClickEvent event) {
                    ErrorPopup.this.hide();
    			}
    		});
            button.setSize("77px", "29px");
            VerticalPanel panel = new VerticalPanel();
            panel.add(new Label(message));
            panel.add(button);
            setWidget(panel);
    //        setHeight("250px");
            setWidth("400px");
            setText(" ");
            setStyleName("gwt-DialogBox");
            setGlassEnabled(true);
    	}
        
    }
}
